// 自定义弹出窗口，对话框 ，等待遮罩
//
//

// 原IE8不支持
//$(function() {
//    $('head').append('<link type="text/css" href="../../Scripts/jquery.window/jqbox.css" rel="stylesheet" />') ;
//})

; (function ($) {

    var defaults = {
        id:"",
        title: "",
        height: 0,
        width: 0,
        method: "Frame", 
        url: "",
        titleCss: {},
        isDrag: false,
        opacity:0.1,
        closeCallback: null,
        cancelBefore: null,
        position: "top",//"top" || "middle"
        cancelCallback:null,
        keepCallback: null
    };

    var windows = window.top || window;
    var documents = windows.document;

    $.extend({
        rmDialog: function () {
            if (arguments[1] == "cancelBefore") {
                var id = arguments[0];
                var $body = $(documents).children().find('body').length ? $(documents).children().find('body') : $(documents).children().find('#rmdialogbody');
                var $contentBox = $body.children("#contentBox_" + id);
                $contentBox[0].settings = $.extend($contentBox[0].settings, { cancelBefore: arguments[2] });
            } else if (arguments[1] == "setDialog") {
                var id = arguments[0];
                var $body = $(documents).children().find('body').length ? $(documents).children().find('body') : $(documents).children().find('#rmdialogbody');
                var $contentBox = $body.children("#contentBox_" + id);
                var $markbox = $body.children("#markbox_" + id);
                var $title = $contentBox.find("#title_" + id).children("span");

                $contentBox[0].settings = $.extend($contentBox[0].settings, arguments[2]);

                var settings = $contentBox[0].settings;
                if (arguments[2].title) {
                    $title.text(arguments[2].title);
                }

                if (arguments[2].titleCss) {
                    $contentBox.find("#title_" + id).css(arguments[2].titleCss);
                }

                if (arguments[2].height || arguments[2].width) {
                    var height = arguments[2].height || settings.height;
                    var width = arguments[2].width || settings.width;
                    $.ChangeDialog($contentBox, height, width, $markbox);
                }

                if (arguments[2].url) {
                    var $iframe = $contentBox.find("#iframe_" + id);
                    $iframe.attr("src", arguments[2].url);
                }

            } else if (arguments[1] == "setParam") {
                var id = arguments[0];
                var $body = $(documents).children().find('body').length ? $(documents).children().find('body') : $(documents).children().find('#rmdialogbody');
                var $contentBox = $body.children("#contentBox_" + id);
                $contentBox[0].param = arguments[2];
            } else if (arguments[1] == "getParam") {
                var id = arguments[0];
                var $body = $(documents).children().find('body').length ? $(documents).children().find('body') : $(documents).children().find('#rmdialogbody');
                var $contentBox = $body.children("#contentBox_" + id);
                return $contentBox[0].param;
            }
            else {
                return false;
            }

        },
        OpenRmDialog: function (options) {
            
            var settings =new Object();
            settings = $.extend(settings, $.extend(defaults, options));

            var WINDOW_HEIGHT = $(windows).height();
            var WINDOW_WIDTH = $(windows).width();

            var $body = $(documents).children().find('body').length ? $(documents).children().find('body') : $(documents).children().find('#rmdialogbody');

            if (!$body.length) {
                
                $body = $('<div id="rmdialogbody" style="background:#ccc; height:200px;"><div>123</div></div>');
                $(documents).children().find('head').after($body);
            }

            var id = settings.id;
            var height = settings.height;
            var width = settings.width;
            var url = settings.url;
            var title = settings.title;
            
            
            //if (!$body.children(".markbox").length) {
                $body.append('<div class="markbox" id="markbox_' + id +'"></div>');
            //}

            var pagehtml = "";
            if (settings.method == "Load") {
                pagehtml = '<div style="border:#C6D4DF 1px solid;background-color: #fff;" ><div id="pageBox_' + id + '" class="pagebox"></div></div>';
            } else {
                pagehtml = '<iframe style="border:1px #C6D4DF solid;border-top:0px;" id="iframe_' + id + '" onload="iframe_' + id + '.focus()"  frameborder="0" src="' + url + '"></iframe>';//
            }

            //$body.append('<div id="contentBox_' + id + '" class="contentbox">' +
            //    '<div id="titleBox_' + id + '" class="titlebox" >' +
            //        '<div id="title_' + id + '" class="dialogtitle">' + title + '</div>' +
            //        '<div id="close_' + id + '" class="dialogclose"></div>' +
            //    '</div>' +
            //    pagehtml +
            //'</div>');

            $body.append('<div id="contentBox_' + id + '" class="contentbox">' +
                '<div id="titleBox_' + id + '" class="titlebox" >' +
                    '<div class="titlebL"><div style="text-align:center;" class="titlebR" id="title_' + id + '"><span>' + title + '</span><a href="javascript:void(0);" onclick="window.top.RmDialoClose(\''+ id +'\');"  id="close_' + id + '"></a></div></div>' +
                '</div>' +
                pagehtml +
            '</div>');

            //markBox
            var $markBox = $body.children("#markbox_" + id);
            $markBox.css({ 'opacity': settings.opacity });
            $markBox.height(WINDOW_HEIGHT + 20).width(WINDOW_WIDTH + 20);

            //contentBox
            var $contentBox = $body.children("#contentBox_" + id);
            $contentBox.height(height).width(width);

            //contentBox position
            var top = (WINDOW_HEIGHT - height) / 2;
            if (settings.position == "top" && top > 100) {
                top = 100;
            }

            $contentBox.css('top', top <0 ? 0 : top );
            $contentBox.css('left', (WINDOW_WIDTH - width) / 2);           

            //pageBox
            var $pageBox;
            if(settings.method == "Load"){
                $pageBox = $contentBox.children("#pageBox_" + id);
            }else{
                $pageBox = $contentBox.children("#iframe_" + id);
            }

            $pageBox.height(height-28);
            $pageBox.width(width - 2);

           
            ////关闭按钮 Cancel
            //$contentBox.delegate("#close_" + id, "click", function () {

            //    console.log("???");
            //    if ($contentBox[0].settings.cancelBefore) {
            //        if ($contentBox[0].settings.cancelBefore()) {
            //            cancel();
            //        } else {
            //            return false;
            //        }
            //    } else {
            //        cancel();
            //    }

            //    function cancel() {
            //        $contentBox.fadeOut(600, function () {
            //            $(this).remove();
            //        });
            //        $markBox.fadeOut(600, function () {
            //            $(this).remove();
            //        });
            //        if (settings.cancelCallback != undefined) {
            //            settings.cancelCallback();
            //        }
            //    }

            //});

            if (settings.method == "Load") {
                //载入页面
                if (url.indexOf("?") >= 0) {
                    url = url.replace("?", "?stemptime=" + (new Date()).getTime() + "&");
                }
                else {
                    url += "?stemptime=" + (new Date()).getTime();
                }
                $pageBox.load(url);
            }

            var $titlebox = $contentBox.children("#titleBox_" + id);

            $contentBox.find("#title_" + id).css(settings.titleCss);

            $contentBox[0].settings = settings;

            //淡入窗口
            $contentBox.fadeIn(500);
            
            //drag
            if (settings.isDrag) {
                var fx;
                var fy;
                $titlebox.mousedown(function (event) {
                    $contentBox.css("cursor", "move");
                    fx = event.pageX;
                    fy = event.pageY;
                    $markBox.mousemove(function (e) {
                        changePosition($contentBox, e);
                    }).mouseup(removemousemove);
                    $titlebox.mousemove(function (e) {
                        changePosition($contentBox, e);
                    }).mouseup(removemousemove);
                    $pageBox.mousemove(function (e) {
                        changePosition($contentBox, e);
                    }).mouseup(removemousemove);
                });
            }

            function removemousemove() {
                $contentBox.css("cursor", "default");
                $pageBox.unbind("mousemove");
                $titlebox.unbind("mousemove");
                $markBox.unbind("mousemove");
            }

            function changePosition($moveDiv, myEvent) {
                myEvent = myEvent || window.event;
                if (fx > myEvent.pageX) {
                    var change_left_x = parseInt($moveDiv.css("left")) - (fx - myEvent.pageX);
                    //测试是否超出左边窗口 
                    if (change_left_x < 5) {
                        change_left_x = 0;
                    }
                    $moveDiv.css("left", change_left_x + "px");
                    fx = myEvent.pageX;
                }
                if (fx < myEvent.pageX) {
                    var change_right_x = parseInt($moveDiv.css("left")) + (myEvent.pageX - fx);
                    //测试是否超出窗口右边 
                    if (change_right_x > ($(windows).width() - parseInt($moveDiv.css("width")) - 5)) {
                        change_right_x = ($(windows).width() - parseInt($moveDiv.css("width")) - 5);
                    }
                    $moveDiv.css("left", change_right_x + "px");
                    fx = myEvent.pageX;
                }
                if (fy > myEvent.pageY) {
                    var change_top_y = parseInt($moveDiv.css("top")) - (fy - myEvent.pageY);
                    //测试是否超出窗口顶部 
                    if (change_top_y < 5) {
                        change_top_y = 0;
                    }
                    $moveDiv.css("top", change_top_y + "px");
                    fy = myEvent.pageY;
                }
                if (fy < myEvent.pageY) {
                    var change_bottom_y = parseInt($moveDiv.css("top")) + (myEvent.pageY - fy);
                    //测试是否超出窗口底部 
                    if (change_bottom_y > ($(windows).height() - parseInt($moveDiv.height()) - 5)) {
                        change_bottom_y = ($(windows).height() - parseInt($moveDiv.height()) - 5);
                    }
                    $moveDiv.css("top", change_bottom_y + "px");
                    fy = myEvent.pageY;
                }
            }

            //window变化
            $(window).resize(change);
            function change() {
                $.ChangeDialog($contentBox, $contentBox[0].settings.height, $contentBox[0].settings.width, $markBox);
            }

        },
        ChangeDialog: function ($contentBox, height, width, $markBox) {
           
            $contentBox.height(height).width(width);
            var top = ($(windows).height() - height) / 2;
            if ($contentBox[0].settings.position == "top" && top > 100) {
                top = 100;
            }
            $contentBox.css('top', top < 0 ? 0 : top );
            $contentBox.css('left', ($(windows).width() - width) / 2);
            $markBox.height($(windows).height() + 20).width($(windows).width() + 20);
            var $iframe = $contentBox.find("#iframe_" + $contentBox.attr("id").split('_')[1]);
            $iframe.height(height - 28);
            $iframe.width(width - 2);
        },
        CloseRmDialog: function (id, dataObj) {

            var $body = $(documents).children().find('body').length ? $(documents).children().find('body') : $(documents).children().find('#rmdialogbody');
            var $contentBox = $body.children("#contentBox_" + id);
            var $markBox = $body.children("#markbox_"+ id);

            //callback
            if ($contentBox[0].settings && $contentBox[0].settings.closeCallback) {
                $contentBox[0].settings.closeCallback(dataObj);
            }

            $markBox.fadeOut(600, function () {
                $(this).remove();
            });

            $contentBox.fadeOut(600, function () {
                $(this).remove();
            });
                        
        },
        FireKeep: function (id, dataObj) {
            var $body = $(documents).children().find('body').length ? $(documents).children().find('body') : $(documents).children().find('#rmdialogbody');
            var $contentBox = $body.children("#contentBox_" + id);

            //callback
            if ($contentBox[0].settings && $contentBox[0].settings.keepCallback) {
                $contentBox[0].settings.keepCallback(dataObj);
            }
        }
    });

    window.top.RmDialoClose = function (id) {

        var $body = $(documents).children().find('body').length ? $(documents).children().find('body') : $(documents).children().find('#rmdialogbody');
        var $contentBox = $body.children("#contentBox_" + id);
        var $markBox = $body.children("#markbox_" + id);
        
        var settings = $contentBox[0].settings;

            if (settings.cancelBefore) {
                if (settings.cancelBefore()) {
                    cancel();
                } else {
                    return false;
                }
            } else {
                cancel();
            }

            function cancel() {
                
                $contentBox.fadeOut(600, function () {
                    //$(this).remove();
                    $contentBox.remove();
                });
                
                $markBox.fadeOut(600, function () {
                    
                    
                });
                $markBox.remove();
                
                if (settings.cancelCallback != undefined) {
                    settings.cancelCallback();
                }
            }

    }


})(jQuery);



function loginBox() {
    var height = 300, width = 400;

    var url = "/AccountManage/TipBoxLogin";
    var windowlogins = window.top || window;
    var documentlogins = windowlogins.document;
    var $body = $(documentlogins).children().find('body');

    $(".thickframe", $body).remove();
    $(".thickdiv", $body).remove();
    $(".thickbox", $body).remove();

    var Window_Height = $(windowlogins).height();
    var Window_Width = $(windowlogins).width();

    var left = (Window_Width - width) / 2;
    var top = (Window_Height - height) / 2;

    //var $iframe = $('<iframe scrolling="no" frameborder="0" marginheight="0" marginwidth="0" id="" class="thickframe"></iframe>');
    var $thickdiv = $('<div id="" class="thickdiv"></div>');
    var $thickbox = $('<div id="" class="thickbox" style="width:' + width + 'px; height: ' + height + 'px; left: ' + left + 'px; top: ' + (top > 100 ? 100 : top) + 'px;">' +
                        '<div style="width: ' + (width - 22) + 'px" id="thicktitler" class="thicktitle"><span>您尚未登录</span></div>' +
                        '<div style="width: ' + (width - 20) + 'px; height: ' + (height - 49) + 'px; padding-left: 10px; padding-right: 10px;" id="thickconr" class="thickcon">' +
                            '<iframe scrolling="no" frameborder="0" style="width: ' + (width - 22) + 'px; height: ' + (height - 49) + 'px; border: 0;" marginheight="0" marginwidth="0" src="' + url + '"></iframe>' +
                        '</div>' +
                        '<a id="thickcloser" class="thickclose" href="javascript:void(0);">×</a>' +
                    '</div>');
    var $topdiv = $('<div id="tipboxcontent" ></div>').appendTo($body);
    $topdiv.append($thickdiv).append($thickbox);//.append($iframe)

    $(".contentbox", $body).hide();

    $("#thickcloser", $body).click(function () {
        
        $(".contentbox", $body).show();
        $(".thickframe", $body).remove();
        $(".thickdiv", $body).remove();
        $(".thickbox", $body).remove();
    });


    $(windowlogins).resize(alterposition);

    function alterposition() {

        //var windowlogins = window.top || window;
        //var documentlogins = windowlogins.document;
        //var $body = $(documentlogins).children().find('body');

        if ($(".thickbox", $body).length > 0) {

            var left = ($(windowlogins).width() - width) / 2;
            var top = ($(windowlogins).height() - height) / 2;
            $(".thickbox", $body).css({ "left": left, "top": top > 100 ? 100 : top });
        }
    }
    return false;
}

function loginClose() {


    var windowlogins = window.top || window;
    var documentlogins = windowlogins.document;
    var $body = $(documentlogins).children().find('body');
    
    $(".contentbox", $body).show();

    $(".thickframe", $body).remove();
    $(".thickdiv", $body).remove();
    $(".thickbox", $body).remove();
    
    $(".contentbox", $body).show();

}

//右下角弹框提示
; (function ($) {

    $.extend({
        tipWindow: function (options) {

            var defaults = {
                id: "",
                title: "",
                height: 0,
                width: 0,
                titleCss:{},
                method: "Frame",
                steptime:null ,
                url: "",
                html:"", //Load 方式生效
                closeCallback: null
            };

            var windows =  window;
            var documents = windows.document;

            var settings = new Object();
            settings = $.extend(settings, $.extend(defaults, options));

            settings.id = "rm_tipwindow";

            if (settings.html) {
                settings.method = "Load";
            }

            var WINDOW_HEIGHT = $(windows).height();
            var WINDOW_WIDTH = $(windows).width();

            var $body = $(documents).children().find('body').length ? $(documents).children().find('body') : $(documents).children().find('#rmtipwindowbody');

            if (!$body.length) {

                $body = $('<div id="rmtipwindowbody" style="background:#ccc; height:200px;"><div>123</div></div>');
                $(documents).children().find('head').after($body);
            }

            if ($body.children("#contentBox_" + settings.id).length > 0)
            {
                return false;
            }

            var id = settings.id;
            var height = settings.height;
            var width = settings.width;
            var url = settings.url;
            var title = settings.title;

            var pagehtml = "";
            if (settings.method == "Load") {
                pagehtml = '<div style="border:#C6D4DF 1px solid;background-color: #fff;border-top:0px;" ><div id="pageBox_' + id + '" class="pagebox"></div></div>';
            } else {
                pagehtml = '<iframe style="border:1px #C6D4DF solid;border-top:0px;" id="iframe_' + id + '" frameborder="0" src="' + url + '"></iframe>';//
            }

            $body.append('<div id="contentBox_' + id + '" class="contentbox">' +
                '<div id="titleBox_' + id + '" class="titlebox" >' +
                    '<div class="titlebL"><div style="text-align:left;" class="titlebR" id="title_' + id + '"><span>' + title + '</span><a href="javascript:void(0);" style="margin:0px;"  id="close_' + id + '"></a></div></div>' +
                '</div>' +
                pagehtml +
            '</div>');

            //contentBox
            var $contentBox = $body.children("#contentBox_" + id);
            $contentBox.height(height).width(width);

            //contentBox position
            $contentBox.css('bottom',3);
            $contentBox.css('right',3);

            //pageBox
            var $pageBox;
            if (settings.method == "Load") {
                
                $pageBox = $contentBox.find("#pageBox_" + id);
            } else {
                $pageBox = $contentBox.children("#iframe_" + id);
            }

            $pageBox.height(height - 28);
            $pageBox.width(width - 2);


            //关闭按钮 Cancel
            $contentBox.delegate("#close_" + id, "click", function () {
                $contentBox.slideUp(600, function () {
                    $(this).remove();
                });
            });

            if (settings.method == "Load") {
                if (!settings.html) {
                    //载入页面
                    if (url.indexOf("?") >= 0) {
                        url = url.replace("?", "?stemptime=" + (new Date()).getTime() + "&");
                    }
                    else {
                        url += "?stemptime=" + (new Date()).getTime();
                    }
                    $pageBox.load(url);
                }
                else
                {
                    $pageBox.html(settings.html);
                }
            }

            var $titlebox = $contentBox.children("#titleBox_" + id);

            $contentBox.find("#title_" + id).css(settings.titleCss);

            $contentBox[0].settings = settings;

            //淡入窗口
            $contentBox.slideDown(500);

            //auto close
            if (settings.steptime){
                setTimeout(function () {
                    $contentBox.slideUp(1000, function () {
                        $contentBox.remove();
                    });
                }, settings.steptime * 1000);
            }

        }
    });
    
})(jQuery);




